package com.magicsoft.app.helper;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.weitown.ui.CustomDialog;

public class PhoneCallHelper {
	public interface PhoneCallListener {
		void toIntent();
	}

	/**
	 * 判断是否支持电话功能
	 * 
	 * @param aContext
	 * @return
	 */
	private static boolean isPhoneDevice(Context aContext) {

		TelephonyManager telephony = (TelephonyManager) aContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		int type = telephony.getPhoneType();
		if (type == TelephonyManager.PHONE_TYPE_NONE) {
			return false;
		} else {
			return true;
		}

	}

	/**
	 * 拨打电话
	 * 
	 * @param aTel
	 * @param aContext
	 * @param phoneCallListener 
	 */
	public static void makePhoneCall(final String aTel, final Context aContext,final PhoneCallListener listener) {

		final CustomDialog dialog = new CustomDialog(aContext,
				R.style.qr_dialog);
		dialog.show();
		// 标题
		TextView tv_title = (TextView) dialog.findViewById(R.id.dialog_title);
		tv_title.setText(aContext.getResources().getString(R.string.call)
				+ aTel);
		// 内容
		TextView tv_content = (TextView) dialog
				.findViewById(R.id.dialog_content);
		tv_content.setText(aContext.getResources().getString(
				R.string.phone_message));
		// 右上角取消
		ImageView imag_cancel = (ImageView) dialog
				.findViewById(R.id.dialog_img_cancel);
		imag_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		// 确定按钮
		Button btn_ok = (Button) dialog.findViewById(R.id.dialog_button_ok);
		btn_ok.setText(aContext.getResources().getString(R.string.phone_ok));
		btn_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				dialog.cancel();

				if (!PhoneCallHelper.isPhoneDevice(aContext)) {
					Toast.makeText(
							aContext,
							aContext.getResources().getString(
									R.string.phone_notcall), Toast.LENGTH_SHORT)
							.show();
					return;
				}

				listener.toIntent();
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ aTel));

				aContext.startActivity(intent);
			}
		});
		// 取消按钮
		Button btn_cancel = (Button) dialog
				.findViewById(R.id.dialog_button_cancel);
		btn_cancel.setText(aContext.getResources().getString(
				R.string.phone_cancel));
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});

	}
	public static void sendSMS(Context aContext,String number, String message){
	    Uri uri = Uri.parse("smsto:" + number);
	    Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
	    sendIntent.putExtra("sms_body", message);
	    aContext.startActivity(sendIntent);
	}
	public static void sendEmail(Context aContext,String email){
		Uri uri=Uri.parse("mailto:" + email);
        Intent MymailIntent=new Intent(Intent.ACTION_SEND,uri);
        aContext.startActivity(Intent.createChooser(MymailIntent, "请选择邮件发送软件"));
	}
}
